//===================================================
//File name: Menu.java			Version: 1.0
//Created: Tue 12 Feb, 2008
//Creator: Shabal Goel
//
//Purpose: Provide an implementation of a Menu as an aggregation of items, options, a title, a prompt, and a depth
//
//Side effects: Changes the Key and/or Description of Item objects
//
//===================================================

import java.util.ArrayList;

public class Menu {
	private ArrayList<Item> items;
	private String title;
	private String prompt;
	private ArrayList<Item> options;
	private int depth;
	
	public Menu (ArrayList<Item> nItems, String aTitle, String aPrompt, ArrayList<Item> nOptions, int aDepth) {
		items = nItems;
		title = aTitle;
		prompt = aPrompt;
		options = nOptions;
		depth = aDepth;
	}
	
	public void addItem (Item anItem) {
		//First, print an error message and exit the system if the input Item already exists
		for (int i = 0; i < items.size(); i++) {
			if (items.get(i).getKey() == anItem.getKey()) {
				System.out.print("Error: Duplicate key");
				System.exit(0);
			}
		}		
		items.add(anItem);
	}
	
	public void removeItem (Item anItem) {
		items.remove(anItem);
	}

	public Item getItem (char aKey) {
		//First, print an error message and exit the system if the input Key does not exist
		int i = 0;
		while (i < items.size() && items.get(i).getKey() != aKey)
			i++;
		if (i == items.size()) {
			System.out.print("Error: Key not found");
			System.exit(0);
		}
		return items.get(i);
	}

	public void setTitle (String aTitle) {
		title = aTitle;
	}

	public void setPrompt (String aPrompt) {
		prompt = aPrompt;
	}
	
	public void setKey(char oldKey, char newKey) {
		//First, print an error message and exit the system if the proposed new key already exists
		for (int i = 0; i < items.size(); i++) {
			if (items.get(i).getKey() == newKey) {
				System.out.print("Error: Duplicate key");
				System.exit(0);
			}
		}
		
		getItem(oldKey).setKey(newKey);
	}

	public void setDesc(char key, String nDesc) {
		getItem(key).setDesc(nDesc);
	}

	//===================================
	//Function name: display
	//Purpose: Display the Menu in vertical text-based form, in the order: Options, Title, Items, Prompt
	//
	//Notes: With an incremental increase in the attribute depth, the menu is shifted right by depth*\t
	//===================================
	public void display () {
		//Step 1: Print the options
		System.out.println("\n");
		for (int i = 0; i < options.size(); i++) {
			printDepth(depth);
			System.out.println(options.get(i));
		}		
		
		//Step 2: Print the title
		System.out.println("");
		printDepth(depth);
		System.out.println("****************************************");
		printDepth(depth);
		System.out.println(title);
		printDepth(depth);
		System.out.println("****************************************");	

		//Step 3: Print the items
		System.out.println("");
		for (int i = 0; i < items.size(); i++) {
			printDepth(depth);
			System.out.println(items.get(i));
		}
		
		//Step 4: Print the prompt
		System.out.println("");
		printDepth(depth);
		System.out.print(prompt + ": ");
	}
	
	//Purpose: Print aDepth * \t to the output stream
	private void printDepth(int aDepth) {
		for (;aDepth > 0; aDepth--)
			System.out.print("\t");
	}
}